<?php


//输出方法
function p($arr){
    echo "<pre>";
    print_r($arr);
    echo "</pre>";die;
}


// 检测输入的验证码是否正确，$code为用户输入的验证码字符串
function check_verify($code){
    $captcha = new \think\captcha\Captcha();
    return $captcha->check($code);
}


//API输出
function show($status,$message,$data = array()){
    $result = array(
        'status' => $status,
        'message' => $message,
        'data' => $data
    );

    exit(json_encode($result));
}


//密码加密
function getMD5Password($str){
    return md5($str.config('MD5_PATH'));
}


//获取用户名
function getAdminUser(){
    return session('TourAdminUser')['username'];
}


/**
 * [树形结构]
 */
function getTree($data,$pid = 0,$level = 0) {
    static $arr = array();
    foreach ($data as $k => $v) {
        if ($v['pid']==$pid) {
            $v['level']=$level;
            $arr[]=$v;
            getTree($data,$v['rule_id'],$level+1);
        }
    }
    return $arr;
}

//权限状态转化
function getRuleStatus($str){
    if($str == 1){
        $res = "<span style='color:red;'>显示</span>";
    }else{
        $res = "<span style='color:green;'>隐藏</span>";
    }
    return $res;
}

//通用状态
function getStatus($str){
    if($str == 1){
        $res = '<span style="color:red;">正常</span>';
    }else{
        $res = '<span style="color:green;">待审</span>';
    }
    return $res;
}

//转化商品分类
function getWareType($id){
    $wareType = config('WareType');
    $arr = '';
    foreach($wareType as $k => $v){
        if($id == $v['type_id']){
            $arr = $v['type_name'];
        }
    }
    return $arr;
}

//转化攻略的分类名称
function getStrategyClass($id){
    $res = '';
    $class = \think\Db::name('strategy_class')->where('status',1)->select();
    foreach($class as $k => $v){
        if($v['id'] == $id){
            $res = $v['class_name'];
        }
    }
    return $res;
}

//转化攻略的分类名称
function getNavClass($id){
    $res = '';
    $class = \think\Db::name('nav')->select();
    foreach($class as $k => $v){
        if($v['id'] == $id){
            $res = $v['nav'];
        }
    }
    return $res;
}
//转化攻略的分类名称
function getNewsClass($id){
    $res = '';
    $class = \think\Db::name('news_type')->where('status',1)->select();
    foreach($class as $k => $v){
        if($v['id'] == $id){
            $res = $v['name'];
        }
    }
    return $res;
}
//转化攻略的分类名称
function getLineClass($id){
    $res = '';
    $class = \think\Db::name('line_class')->select();
    foreach($class as $k => $v){
        if($v['id'] == $id){
            $res = $v['line_class_name'];
        }
    }
    return $res;
}

//获取角色名称
function getGroupName($id){
    $res = '';
    $group = \think\Db::name('group')->where('group_status',1)->select();
    foreach($group as $k => $v){
        if($v['group_id'] == $id){
            $res = $v['group_name'];
        }
    }
    return $res;
}

/**
 * [百度编辑器返回接口]
 */
function kindShow($status,$data){
    header('Content-type:application/json;charset=UTF-8');
    if($status==0) {
        exit(json_encode(array('error'=>0,'url'=>$data)));
    }
    exit(json_encode(array('error'=>1,'message'=>'上传失败')));
}


//转化性别
function getGender($str){
    if($str == 1){
        $res = '<span>男</span>';
    }else{
        $res = '<span>女</span>';
    }

    return $res;
}

//转化广告分类
function getAdvertType($id){
    $res = '';
    $type = \think\Db::name('advert_type')->where('type_status',1)->select();
    foreach($type as $k =>$v){
        if($v['type_id'] == $id){
            $res = $v['type_name'];
        }
    }
    return $res;
}

//转化订单
function getOrderStatus($id){

    if($id == 1){
        $res = '待付款';
    }else if($id == 2){
        $res = '<span style="color:red;">待使用</span>';
    }else{
        $res = '<span style="color:green;">已完成</span>';
    }

    return $res;
}

//转化团购
function getCollageStatus($id){
    if($id == 1){
        $res = '拼团中';
    }else if($id == 2){
        $res = '<span style="color:red;">拼团失败</span>';
    }else{
        $res = '拼团成功';
    }
    return $res;
}

//团购团购状态
function getMemberStatus($str){
    if($str == 0){
        $res = '<span>未支付</span>';
    }else if($str == 1){
        $res = '<span>待使用</span>';
    }else if($str == 2){
        $res = '<span style="color:red;">已退款</span>';
    }else{
        $res = '<span style="color:green;">已使用</span>';
    }

    return $res;
}
